package com.gitee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.bo.RoleBo;
import com.gitee.enums.ResponseEnum;
import com.gitee.exception.AdminException;
import com.gitee.mapper.RoleMapper;
import com.gitee.pojo.Role;
import com.gitee.service.IRolePermissionService;
import com.gitee.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private IRolePermissionService rolePermissionService;


    /**
     * 添加角色
     *
     * @param roleBo
     * @return
     */
    @Override
    public Boolean addRole(RoleBo roleBo) {
        LambdaQueryWrapper<Role> queryWrapper = new QueryWrapper<Role>().lambda().eq(Role::getTitle, roleBo.getTitle());
        if (super.count(queryWrapper) > 0) {
            throw new AdminException(ResponseEnum.EXIST_ROLE);
        }
        Role role = new Role();
        role.setTitle(roleBo.getTitle());
        return super.save(role);
    }

    /**
     * 修改角色
     *
     * @param roleBo
     * @param roleId
     * @returnr
     */
    @Override
    public Boolean updateRole(RoleBo roleBo, Integer roleId) {
        LambdaQueryWrapper<Role> queryWrapper = new QueryWrapper<Role>().lambda().eq(Role::getTitle, roleBo.getTitle());
        if (super.count(queryWrapper) > 0) {
            throw new AdminException(ResponseEnum.EXIST_ROLE);
        }
        Role role = new Role();
        role.setId(roleId);
        role.setTitle(roleBo.getTitle());
        return super.updateById(role);
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @Override
    public Boolean deleteRole(Integer roleId) {
        return super.removeById(roleId) && rolePermissionService.deleteByRoleId(roleId);
    }
}
